#!/bin/bash

# 定义程序名称
#PROGRAM="MoSkeleton.adm64.bin"
PROGRAM=$2
if [ -z $PROGRAM ];then
    PROGRAM=`ls -t |grep .bin$ |head -n1`
fi
echo "program: $PROGRAM"

# 检查参数数量
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 {start|stop|restart} [MoSkeleton.adm64.bin]"
    exit 1
fi

case $1 in
    start)
        if pgrep -x "$PROGRAM" > /dev/null
        then
            echo "$PROGRAM is already running."
        else
            nohup ./$PROGRAM > /dev/null 2>&1 &
            echo "$PROGRAM started."
        fi
        ;;
    stop)
        if pgrep -x "$PROGRAM" > /dev/null
        then
            pkill -x "$PROGRAM"
            echo "$PROGRAM stopped."
        else
            echo "$PROGRAM is not running."
        fi
        ;;
    restart)
        if pgrep -x "$PROGRAM" > /dev/null
        then
            pkill -x "$PROGRAM"
            echo "$PROGRAM stopped."
        fi
        nohup ./$PROGRAM > /dev/null 2>&1 &
        echo "$PROGRAM restarted."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart} [MoSkeleton.adm64.bin]"
        exit 1
        ;;
esac

exit 0
